package com.sky.base;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class BaseSwitch {
	public static BufferedImage expandImage(BufferedImage image){
		BufferedImage newImage = new BufferedImage(image.getWidth()*2, image.getHeight()*2,BufferedImage.TYPE_INT_RGB);
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				newImage.setRGB(i*2,j*2,image.getRGB(i, j));
				newImage.setRGB(i*2,j*2+1,image.getRGB(i, j));
			}
		for(int i = 1;i < newImage.getWidth();i+=2)
			for(int j = 0;j < newImage.getHeight();j++)
				newImage.setRGB(i, j, newImage.getRGB(i-1, j));
		return newImage;
	}
	public static BufferedImage nearetExpand(BufferedImage image,int x,int y){
		BufferedImage newImage = new BufferedImage(x,y,BufferedImage.TYPE_INT_RGB);
		double x_factor = (double)image.getWidth()/x;
		double y_factor = (double)image.getHeight()/y;
		for(int i = 0;i < x;i++)
			for(int j = 0;j < y;j++){
				newImage.setRGB(i,j,image.getRGB((int)(i*x_factor), (int)(j*y_factor)));
			}
		return newImage;
	}
	public static BufferedImage doubleLinerExpand(BufferedImage image,int x,int y){
		BufferedImage newImage = new BufferedImage(x,y,BufferedImage.TYPE_BYTE_GRAY);
		double x_factor = (double)image.getWidth()/x;
		double y_factor = (double)image.getHeight()/y;
		
		for(int i = 0;i < x;i++)
			for(int j = 0;j < y;j++){
				newImage.setRGB(i,j,getRGB(image,i*x_factor,j*y_factor,(int)(i*x_factor),(int)(j*y_factor)));
			}
		return newImage;
	}
	
	public static BufferedImage randImage(BufferedImage image,int time){
		for(int i = 0;i < time;i++){
			image = randImage(image);
		}
		return image;
	}
	
	public static BufferedImage averageImage(ArrayList<BufferedImage> l){
		BufferedImage newImage = new BufferedImage(l.get(0).getWidth(),l.get(0).getHeight(),BufferedImage.TYPE_BYTE_GRAY);
		for(int i = 0;i < newImage.getWidth();i++)
			for(int j = 0;j < newImage.getHeight();j++){
				int rgb = 0;
				for(BufferedImage image:l){
					rgb += ((image.getRGB(i, j) & 0xff00 )>> 8);
				}
				rgb = rgb/l.size() & 0xff;
				rgb = rgb | rgb << 8 | rgb << 16;
				newImage.setRGB(i, j, rgb);
			}
		return newImage;
	}
	
	public static BufferedImage imageSub(BufferedImage image1,BufferedImage image2){
		BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				int rgb1 = image1.getRGB(i, j) & 0xff00;
				int rgb2 = image2.getRGB(i, j) & 0xff00;
				int rgb = (rgb1 - rgb2) & 0xff00;
				rgb = rgb | rgb << 8 | rgb >> 8;
				image.setRGB(i, j, rgb);
			}
		return image;
	}
	
	public static BufferedImage standIamge(BufferedImage image){
		BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
		int min = image.getRGB(0, 0) & 0xff00;
		int max = image.getRGB(0, 0) & 0xff00;
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				int rgb = image.getRGB(i, j) & 0xff00;
				min = Math.min(min, rgb);
				max = Math.max(max, rgb);
			}
		
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				int rgb = image.getRGB(i, j) & 0xff00;
				rgb = (int)(256*((rgb-min)/(max+0.000001)));
				rgb = rgb | rgb << 8 | rgb >> 8;
				newImage.setRGB(i, j, rgb);
			}
		return newImage;
	}
	
	public static BufferedImage imageDivision(BufferedImage image1,BufferedImage image2){
		BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				int rgb1 = image1.getRGB(i, j) & 0xff00;
				int rgb2 = image2.getRGB(i, j) & 0xff00;
				int rgb = (int)(rgb1/(rgb2+0.00001)) & 0xff00;
				rgb = rgb | rgb << 8 | rgb >> 8;
				image.setRGB(i, j, rgb);
			}
		return image;
	}
	
	public static BufferedImage grayImage(int width, int height) {
		BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_BYTE_GRAY);
		for(int i = 0;i < image.getWidth();i++)
			for(int j = 0;j < image.getHeight();j++){
				int rgb = (int)(Math.random()*15);
				rgb = rgb & 0xff;
				rgb = rgb | rgb << 8 | rgb << 16;
				image.setRGB(i, j, rgb);
			}
		return image;
	}
	
	private static BufferedImage randImage(BufferedImage image){
		BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
		for(int i = 0;i < newImage.getWidth();i++)
			for(int j = 0;j < newImage.getHeight();j++){
				int rgb = (image.getRGB(i, j) & 0xff00 )>> 8;
				rgb = (rgb + (int)(16*Math.random())) & 0xff;
				rgb = rgb | rgb << 8 | rgb << 16;
				newImage.setRGB(i, j, rgb);
			}
		return newImage;
	}
	
	private static int getRGB(BufferedImage image, double x, double y,int nearest_x,int nearest_y) {
		
		int[] zone = new int[4];
		double det_x =  x - nearest_x;
		double det_y =  y - nearest_y;

		zone[0] = image.getRGB(nearest_x, nearest_y) & 0xff00;
		if(nearest_x+1 < image.getWidth() && nearest_y+1 < image.getHeight()){ 
			zone[1] = image.getRGB(nearest_x+1, nearest_y) & 0xff00;
			zone[2] = image.getRGB(nearest_x, nearest_y+1) & 0xff00;
			zone[3] = image.getRGB(nearest_x+1, nearest_y+1) & 0xff00;
		}else{
			zone[1] = image.getRGB(nearest_x, nearest_y) & 0xff00;
			zone[2] = image.getRGB(nearest_x, nearest_y) & 0xff00;
			zone[3] = image.getRGB(nearest_x, nearest_y) & 0xff00;
		}
		int result= (int)(det_x*(zone[1] - zone[0]) + det_y*(zone[2] - zone[0]) + 
						det_x*det_y*(zone[3] + zone[0] - zone[1] - zone[2]) + zone[0]); 
		result &= 0x00ff00;
		return result | (result << 8) | (result >> 8);
	}
	
//	private static int[][] getRpgData(BufferedImage image){
//		int[][] rpgData = new int[image.getHeight()][image.getWidth()];
//		for(int i = 0;i < image.getHeight();i++)
//			for(int j = 0;j < image.getWidth();j++)
//				rpgData[i][j] = image.getRGB(i, j);
//		return rpgData;
//	}
}
